/**
 * Created by Frings on 2017/5/2.
 */
var Pager = function (content_ele, pager_ele, url) {
    this.content_ele = content_ele;
    this.pager_ele = pager_ele;
    this.url = url;
}
Pager.prototype.Render = function (searchFun) {
    //绑定查询按钮事件
    this.content_ele.find(".search-icon").unbind().click(function () {
        eval(searchFun + "()");
    });

    var row_count = this.content_ele.find("#row_count").val();
    var page_size = this.content_ele.find("#page_size").val();
    var current_page = this.content_ele.find("#current_page").val();

    var html = '';
    html += '<div class="pagination-content">';
    html += '<div class="pagination-total-count">共 ' + row_count + ' 条数据</div>';
    html += '<div class="pagination-page-size">';
    html += '每页显示';
    html += '<select id="pagination_page_size_select" onchange="' + searchFun + '()">';
    html += '<option value="10"' + (page_size == 10 ? 'selected="selected"' : '') + '>10</option>';
    html += '<option value="20"' + (page_size == 20 ? 'selected="selected"' : '') + '>20</option>';
    html += '<option value="30"' + (page_size == 30 ? 'selected="selected"' : '') + '>30</option>';
    html += '<option value="50"' + (page_size == 40 ? 'selected="selected"' : '') + '>50</option>';
    html += '</select>';
    html += '条数据';
    html += '</div>';
    html += '<div class="pagination-link-pager">';
    html += '<ul class="pagination">';
    if (current_page == 1) {
        html += '<li class="prev disabled"><span>«</span></li>';
    } else {
        html += '<li class="prev"><a href="javascript:' + searchFun + '(1)">«</a></li>';
    }

    if (parseInt(row_count / page_size) + 1 <= 10) {
        for (var i = 1; i <= parseInt(row_count / page_size) + 1; i++) {
            html += '<li ' + (i == current_page ? 'class="active"' : '') + '><a href="javascript:' + searchFun + '(' + i + ')" >' + i + '</a></li>';
        }
    } else {
        var begin = 1;
        var end = parseInt(row_count / page_size) + 1;
        if (current_page - begin >= 5) {
            begin = current_page - 5;
        }
        if (end - current_page >= 4) {
            end = current_page + 4 + (5 - (current_page - begin));
        }

        for (var i = begin; i <= end; i++) {
            html += '<li ' + (i == current_page ? 'class="active"' : '') + '><a href="javascript:' + searchFun + '(' + i + ')" >' + i + '</a></li>';
        }
    }

    if (current_page < parseInt(row_count / page_size) + 1) {
        html += '<li class="next"><a href="javascript:' + searchFun + '(' + (parseInt(current_page) + 1) + ')" >»</a></li>';
    } else {
        html += '<li class="next disabled"><span>»</span></li>';
    }


    html += '</ul> ';
    html += '</div>';
    html += '<div class="pagination-total-page">共 ' + (parseInt(row_count / page_size) + 1) + ' 页</div>';
    html += '<input type="number" id="pagination_page_input" class="pagination-page-input" value="1">';
    html += '<input type="button" class="btn btn-primary pagination-page-btn" value="跳转" onclick="' + searchFun + '()">';
    html += '</div>';


    this.pager_ele.html(html);
}